﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKArch.Utils;
using HYLightingEquipmentLay.SmokeDetectorLayout;
using RegionSplittor.Data;

namespace RegionSplittor.Utils
{
	public class BeamRegionPostionGetter
	{
		public static List<XYZ> GetBeamRegionLayoutPoints(List<BeamRegionData> arRegion)
		{
			List<XYZ> list = new List<XYZ>();
			GarageLineParam garageInterFaceInfo = new HVACUserSetting().GetGarageInterFaceInfo();
			bool flag = false;
			if (!garageInterFaceInfo.SZLLayStyle.Contains("居中"))
			{
				flag = true;
			}
			List<BeamRegionData> list2 = new List<BeamRegionData>();
			foreach (BeamRegionData beamRegionData in arRegion)
			{
				int nCellCount = beamRegionData.m_nCellCount;
				if (nCellCount >= 1)
				{
					switch (nCellCount)
					{
					case 1:
						list.AddRange(BeamRegionPostionGetter.GetOneBeamCellLayoutPoints(beamRegionData));
						continue;
					case 2:
						list.AddRange(BeamRegionPostionGetter.GetTwoBeamCellLayoutPoints(beamRegionData, garageInterFaceInfo));
						continue;
					case 3:
						list.AddRange(BeamRegionPostionGetter.GetThreeBeamCellLayoutPoints(beamRegionData));
						continue;
					case 4:
						if (!flag)
						{
							list.AddRange(BeamRegionPostionGetter.GetFourBeamCellLayoutPoints(beamRegionData));
							continue;
						}
						list2.Add(beamRegionData);
						continue;
					case 6:
						list.AddRange(BeamRegionPostionGetter.GetSixBeamCellLayoutPoints(beamRegionData));
						continue;
					case 9:
						list.AddRange(BeamRegionPostionGetter.GetNineBeamCellLayoutPoints(beamRegionData, garageInterFaceInfo));
						continue;
					}
					list.AddRange(BeamRegionPostionGetter.GetNotRuleLayoutPoints(beamRegionData));
				}
			}
			if (flag && list2.Count > 0)
			{
				List<BeamArea> list3 = new List<BeamArea>();
				foreach (BeamRegionData beamRegion in list2)
				{
					list3.Add(new BeamArea(beamRegion));
				}
				list.AddRange(CrossLayout.GetDetectorLayoutPos(list3, garageInterFaceInfo));
			}
			return list;
		}

		private static List<XYZ> GetNotRuleLayoutPoints(BeamRegionData region)
		{
			List<XYZ> list = new List<XYZ>();
			if (!region.m_bHasSecondary)
			{
				using (List<BeamCellData>.Enumerator enumerator = region.m_arBeamCell.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						BeamCellData beamCellData = enumerator.Current;
						double dFloorHeight = beamCellData.m_dFloorHeight;
						XYZ ptCenter = beamCellData.m_ptCenter;
						list.Add(new XYZ(ptCenter.X, ptCenter.Y, dFloorHeight));
					}
					return list;
				}
			}
			XYZ xyz = null;
			double num = 0.0;
			XYZ xyz2 = null;
			double num2 = 0.0;
			BeamCellData maxBeamCell = region.GetMaxBeamCell();
			if (maxBeamCell != null)
			{
				xyz = maxBeamCell.m_ptCenter;
				num = maxBeamCell.m_dFloorHeight;
			}
			if (xyz != null)
			{
				SortedDictionary<double, BeamCellData> sortedDictionary = new SortedDictionary<double, BeamCellData>();
				foreach (BeamCellData beamCellData2 in region.m_arBeamCell)
				{
					double key = xyz.DistanceTo(beamCellData2.m_ptCenter);
					sortedDictionary.Add(key, beamCellData2);
				}
				if (sortedDictionary.Count > 0)
				{
					xyz2 = sortedDictionary.Last<KeyValuePair<double, BeamCellData>>().Value.m_ptCenter;
					num2 = sortedDictionary.Last<KeyValuePair<double, BeamCellData>>().Value.m_dFloorHeight;
				}
			}
			if (xyz != null)
			{
				list.Add(new XYZ(xyz.X, xyz.Y, num));
			}
			if (xyz2 != null)
			{
				list.Add(new XYZ(xyz2.X, xyz2.Y, num2));
			}
			return list;
		}

		private static List<XYZ> GetOneBeamCellLayoutPoints(BeamRegionData region)
		{
			List<XYZ> list = new List<XYZ>();
			foreach (BeamCellData beamCellData in region.m_arBeamCell)
			{
				XYZ ptCenter = beamCellData.m_ptCenter;
				double dFloorHeight = beamCellData.m_dFloorHeight;
				list.Add(new XYZ(ptCenter.X, ptCenter.Y, dFloorHeight));
			}
			return list;
		}

		private static List<XYZ> GetTwoBeamCellLayoutPoints(BeamRegionData region, GarageLineParam glParam)
		{
			List<XYZ> list = new List<XYZ>();
			if (region.m_nCellCount != 2)
			{
				return list;
			}
			BeamCellData beamCellData = null;
			BeamCellData beamCellData2 = region.m_arBeamCell.First<BeamCellData>();
			BeamCellData beamCellData3 = region.m_arBeamCell.Last<BeamCellData>();
			XYZ ptCenter = beamCellData2.m_ptCenter;
			XYZ ptCenter2 = beamCellData3.m_ptCenter;
			BeamCellData beamCellData4;
			BeamCellData beamCellData5;
			if (ptCenter.Y > ptCenter2.Y)
			{
				beamCellData4 = beamCellData2;
				beamCellData5 = beamCellData3;
			}
			else if (Math.Abs(ptCenter.Y - ptCenter2.Y) < BeamRegionPostionGetter.dZeroTol)
			{
				if (ptCenter.X < ptCenter2.X)
				{
					beamCellData4 = beamCellData2;
					beamCellData5 = beamCellData3;
				}
				else
				{
					beamCellData4 = beamCellData3;
					beamCellData5 = beamCellData2;
				}
			}
			else
			{
				beamCellData4 = beamCellData3;
				beamCellData5 = beamCellData2;
			}
			string yzldirection = glParam.YZLDirection;
			OneWordLayoutType oneWordLayoutType;
			if (yzldirection.Contains("上"))
			{
				oneWordLayoutType = OneWordLayoutType.OWLT_TopLft;
			}
			else if (yzldirection.Contains("下"))
			{
				oneWordLayoutType = OneWordLayoutType.OWLT_BtmRht;
			}
			else
			{
				oneWordLayoutType = OneWordLayoutType.OWLT_NotValid;
			}
			if (oneWordLayoutType == OneWordLayoutType.OWLT_TopLft)
			{
				beamCellData = beamCellData4;
			}
			else if (OneWordLayoutType.OWLT_BtmRht == oneWordLayoutType)
			{
				beamCellData = beamCellData5;
			}
			if (beamCellData == null)
			{
				return list;
			}
			List<CurveElementData> beamCurveData = beamCellData.GetBeamCurveData(BeamOrderType.BT_SecondaryBeam);
			if (beamCurveData.Count > 0)
			{
				XYZ pt = new XYZ(beamCellData.m_ptCenter.X, beamCellData.m_ptCenter.Y, 0.0);
				Curve curve = beamCurveData.First<CurveElementData>().m_Curve;
				XYZ hyendPoint = curve.GetEndPoint(0);
				XYZ hyendPoint2 = curve.GetEndPoint(1);
				XYZ xyz = Geometry.CalculatMidPoint(hyendPoint, hyendPoint2);
				XYZ xyz2 = Geometry.GetCurveUnitNormal(curve, pt);
				double val = double.Parse(glParam.YZLDistance);
				xyz2 = xyz2.Multiply(Common.MMtoIntch(val));
				XYZ xyz3 = xyz + xyz2;
				double dFloorHeight = beamCellData.m_dFloorHeight;
				XYZ item = new XYZ(xyz3.X, xyz3.Y, dFloorHeight);
				list.Add(item);
			}
			return list;
		}

		private static List<XYZ> GetThreeBeamCellLayoutPoints(BeamRegionData region)
		{
			List<XYZ> list = new List<XYZ>();
			bool flag = false;
			List<ElementId> beamIdsByType = region.GetBeamIdsByType(BeamOrderType.BT_SecondaryBeam);
			if (beamIdsByType.Count == 2)
			{
				Line line = region.GetCurveByEleId(beamIdsByType.First<ElementId>()) as Line;
				Line line2 = region.GetCurveByEleId(beamIdsByType.Last<ElementId>()) as Line;
                if (null != line && null != line2 && (int)line.Intersect(line2) == 4 && Geometry.IsParallel(line.Direction, line2.Direction))
				{
					flag = true;
				}
			}
			if (flag)
			{
				List<XYZ> list2 = new List<XYZ>();
				foreach (BeamCellData beamCellData in region.m_arBeamCell)
				{
					XYZ ptCenter = beamCellData.m_ptCenter;
					list2.Add(ptCenter);
				}
				List<XYZ> maxDistancePoints = BeamRegionPostionGetter.GetMaxDistancePoints(list2);
				BeamCellData beamCellData2 = null;
				foreach (BeamCellData beamCellData3 in region.m_arBeamCell)
				{
					XYZ ptCenter2 = beamCellData3.m_ptCenter;
					if (!maxDistancePoints.Contains(ptCenter2))
					{
						beamCellData2 = beamCellData3;
						break;
					}
				}
				if (beamCellData2 != null)
				{
					double dFloorHeight = beamCellData2.m_dFloorHeight;
					XYZ ptCenter3 = beamCellData2.m_ptCenter;
					list.Add(new XYZ(ptCenter3.X, ptCenter3.Y, dFloorHeight));
				}
			}
			else
			{
				list = BeamRegionPostionGetter.GetNotRuleLayoutPoints(region);
			}
			return list;
		}

		private static List<XYZ> GetFourBeamCellLayoutPoints(BeamRegionData region)
		{
			return BeamRegionPostionGetter.GetDiagonallyLayoutPoints(region);
		}

		private static List<XYZ> GetSixBeamCellLayoutPoints(BeamRegionData region)
		{
			new List<XYZ>();
			return BeamRegionPostionGetter.GetDiagonallyLayoutPoints(region);
		}

		private static List<XYZ> GetDiagonallyLayoutPoints(BeamRegionData region)
		{
			List<XYZ> list = new List<XYZ>();
			List<BeamCellData> maxDistanceCellData = BeamRegionPostionGetter.GetMaxDistanceCellData(region.m_arBeamCell);
			if (2 == maxDistanceCellData.Count)
			{
				foreach (BeamCellData beamCellData in maxDistanceCellData)
				{
					double dFloorHeight = beamCellData.m_dFloorHeight;
					XYZ ptCenter = beamCellData.m_ptCenter;
					list.Add(new XYZ(ptCenter.X, ptCenter.Y, dFloorHeight));
				}
			}
			return list;
		}

		private static List<XYZ> GetNineBeamCellLayoutPoints(BeamRegionData region, GarageLineParam glParam)
		{
			List<XYZ> list = new List<XYZ>();
			try
			{
				foreach (BeamCellData beamCellData in BeamRegionPostionGetter.GetMaxDistanceCellData(region.m_arBeamCell))
				{
					List<ElementId> beamIdsByType = beamCellData.GetBeamIdsByType(BeamOrderType.BT_SecondaryBeam);
					if (beamIdsByType.Count == 2)
					{
						List<Curve> cellCurveById = beamCellData.GetCellCurveById(beamIdsByType);
						if (cellCurveById.Count == 2)
						{
							XYZ pt = new XYZ(beamCellData.m_ptCenter.X, beamCellData.m_ptCenter.Y, 0.0);
							Line line = cellCurveById.First<Curve>() as Line;
							Line line2 = cellCurveById.Last<Curve>() as Line;
							if (!(null == line) && !(null == line2))
							{
								double num = Common.MMtoIntch(double.Parse(glParam.JZLDistance));
								XYZ xyz = Geometry.GetCurveUnitNormal(line, pt).Multiply(num);
								XYZ xyz2 = Geometry.GetCurveUnitNormal(line2, pt).Multiply(num);
								Transform transform = Transform.CreateTranslation(xyz);
								Transform transform2 = Transform.CreateTranslation(xyz2);
								Curve curve = line.CreateTransformed(transform);
								Curve curve2 = line2.CreateTransformed(transform2);
								IntersectionResultArray intersectionResultArray = null;
								curve.Intersect(curve2, out intersectionResultArray);
								if (intersectionResultArray != null)
								{
									XYZ xyzpoint = intersectionResultArray.get_Item(0).XYZPoint;
									list.Add(new XYZ(xyzpoint.X, xyzpoint.Y, beamCellData.m_dFloorHeight));
								}
							}
						}
					}
				}
			}
			catch
			{
			}
			return list;
		}

		private static List<XYZ> GetMaxDistancePoints(List<XYZ> arPoints)
		{
			List<XYZ> list = new List<XYZ>();
			SortedDictionary<double, List<XYZ>> sortedDictionary = new SortedDictionary<double, List<XYZ>>();
			foreach (XYZ xyz in arPoints)
			{
				foreach (XYZ xyz2 in arPoints)
				{
					if (!xyz.Equals(xyz2))
					{
						double key = xyz.DistanceTo(xyz2);
						if (!sortedDictionary.ContainsKey(key))
						{
							sortedDictionary.Add(key, new List<XYZ>
							{
								xyz,
								xyz2
							});
						}
					}
				}
			}
			if (sortedDictionary.Count > 0)
			{
				list.AddRange(sortedDictionary.Last<KeyValuePair<double, List<XYZ>>>().Value);
			}
			return list;
		}

		private static List<BeamCellData> GetMaxDistanceCellData(List<BeamCellData> arBeamCell)
		{
			List<BeamCellData> list = new List<BeamCellData>();
			SortedDictionary<double, List<BeamCellData>> sortedDictionary = new SortedDictionary<double, List<BeamCellData>>();
			if (arBeamCell.Count == 4)
			{
				List<BeamCellData> longDisCell = BeamRegionPostionGetter.GetLongDisCell(arBeamCell);
				sortedDictionary.Add(4.0, longDisCell);
			}
			else if (arBeamCell.Count == 9)
			{
				List<BeamCellData> longDisCell2 = BeamRegionPostionGetter.GetLongDisCell(arBeamCell);
				sortedDictionary.Add(9.0, longDisCell2);
			}
			else if (arBeamCell.Count == 6)
			{
				List<BeamCellData> longDisCell3 = BeamRegionPostionGetter.GetLongDisCell(arBeamCell);
				sortedDictionary.Add(6.0, longDisCell3);
			}
			else
			{
				foreach (BeamCellData beamCellData in arBeamCell)
				{
					XYZ ptCenter = beamCellData.m_ptCenter;
					foreach (BeamCellData beamCellData2 in arBeamCell)
					{
						XYZ ptCenter2 = beamCellData2.m_ptCenter;
						if (!ptCenter.Equals(ptCenter2))
						{
							double key = ptCenter.DistanceTo(ptCenter2);
							if (!sortedDictionary.ContainsKey(key))
							{
								sortedDictionary.Add(key, new List<BeamCellData>
								{
									beamCellData,
									beamCellData2
								});
							}
						}
					}
				}
			}
			if (sortedDictionary.Count > 0)
			{
				list.AddRange(sortedDictionary.Last<KeyValuePair<double, List<BeamCellData>>>().Value);
			}
			return list;
		}

		private static List<BeamCellData> GetLongDisCell(List<BeamCellData> arBeamCell)
		{
			List<BeamCellData> list = new List<BeamCellData>();
			double num = 0.0;
			foreach (BeamCellData beamCellData in arBeamCell)
			{
				foreach (BeamCellData beamCellData2 in arBeamCell)
				{
					double num2 = beamCellData.m_ptCenter.DistanceTo(beamCellData2.m_ptCenter);
					if (num2 > num)
					{
						num = num2;
						list.Clear();
						list.Add(beamCellData);
						list.Add(beamCellData2);
					}
				}
			}
			return list;
		}

		public static double dZeroTol = 1E-05;
	}
}
